//
//  LS_WelcomeViewController.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/23.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class LS_WelcomeViewController: UIViewController {
    override func loadView() {
        view = backView
//        backView.backgroundColor = UIColor.redColor()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //加载UI
        
        setUpUI()
    }
    //MARK:加载子视图 自动布局
    private func setUpUI(){
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
//            make.height.width.equalTo(90)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        //下载网络头像
    iconView.sd_setImageWithURL(LS_UserAccountViewModel.sharedAccountViewModel.headURL)
        //设置圆角
        iconView.cornerRadius = 45
        //设置nameLabel的透明度
        nameLabel.alpha = 0
    
    }
    //动画效果建议放在viewDidAppear阶段执行
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    //MARK:添加动画
    private func startAnimation(){
    //弹簧效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offset = -ScreenHeight + 160
        
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: {
            //修改约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            //提前刷新父视图
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    
                    }, completion: { (_) in
                        //发出切换根视图的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                })
        }
    }    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    //MARK:懒加载子视图
    //背景视图
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户名称文案
    private lazy var nameLabel:UILabel = UILabel(title: LS_UserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor(), alignment: .Center)
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
   }
